package com.gdcp.controller.portal;

import com.gdcp.beans.Order;
import com.gdcp.beans.OrderItem;
import com.gdcp.beans.User;
import com.gdcp.common.ResponseModel;
import com.gdcp.config.RequestHolder;
import com.gdcp.service.OrderItemService;
import com.gdcp.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 杨华京
 * @version 1.0
 * @description
 * @since 1.8
 */
@Controller
@RequestMapping("/render/orderitem/")
public class OrderItemController {

    @Autowired
    ResponseModel responseModel;
    @Autowired
    OrderItemService orderItemService;
    @Autowired
    ShopService shopService;

    @Transactional(rollbackFor = { Exception.class })
    @RequestMapping(value = "/changeStatus",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> changeStatus(Integer status, Integer orderitem_id){
        if (status==0||status==10||status==20||status==40||status==50){
            OrderItem orderItem=new OrderItem();
            orderItem.setId(orderitem_id);
            orderItem.setUser_status(status);
            orderItem.setUpdate_time(new Date());
            if (status==10){
                orderItem.setShop_status(status);
            }else if (status==0){
                orderItem.setShop_status(60);
            }else if (status==20){
                orderItem.setShop_status(30);
            }else if (status==50){
                orderItem.setShop_status(50);
            }
            int orderItem_count=orderItemService.updateInformation(orderItem);
            if (orderItem_count<=0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return responseModel.setModel(false,status);
            }

            OrderItem orderItem1=orderItemService.getInformation(orderitem_id);
            int saleCount=shopService.updateSaleCount(orderItem1.getShop_id());
            if (saleCount<=0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return responseModel.setModel(false,status);
            }
            return responseModel.setModel(true,status);
        }

        return responseModel.setModel(false,status);
    }
}
